package com.bckj.fastboot.flow.process;

import com.bckj.fastboot.core.operation.context.OperationLinkedHashMap;
import com.bckj.fastboot.core.util.ExecutionContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class BpmnElResolver {
    private static final ExpressionParser PARSER = new SpelExpressionParser();
    public <T> T getValue(ExecutionContext context, String expression, Class<T> clazz) {
        StandardEvaluationContext evaluationContext = new StandardEvaluationContext(context);
        if (context instanceof OperationLinkedHashMap opMap) {
            opMap.getMap().forEach(evaluationContext::setVariable);
        }
        // 解析表达式
        Expression exp = PARSER.parseExpression(expression);
        return exp.getValue(evaluationContext, clazz);
    }
}
